package microservice.demo.servicecomb.course.edge.filter;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.swagger.invocation.Response;

public class DemoFilter implements HttpServerFilter {
  private static final String LET_STRANGER_PASS = "Let-Stranger-Pass";

  @Override
  public int getOrder() {
    return 0;
  }

  @Override
  public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
    // 从请求中取出名为 Let-Stranger-Pass 的 header
    String letStrangerPass = requestEx.getHeader(LET_STRANGER_PASS);
    if (!StringUtils.isEmpty(letStrangerPass)) {
      // 如果此 header 存在则将它存入到 InvocationContext 中。
      // InvocationContext 可以从上游微服务自动随请求传递给下游服务，该过程由框架自动完成。
      invocation.addContext(LET_STRANGER_PASS, letStrangerPass);
    }
    return null;
  }
}
